
//---------------------------------------------------------------------------
#include "entities/pg1_entity2d.h"
//---------------------------------------------------------------------------
#include "rendering/pg1_rendertypes.h"
#include "rendering/pg1_renderer.h"
#include <d3dx9.h>
//---------------------------------------------------------------------------
using namespace pg1;
//---------------------------------------------------------------------------
Entity2D::Entity2D ()
:
m_fPosX(0.0f),
m_fPosY(0.0f),
m_fRotation(0.0f),
m_fScaleX(1.0f),
m_fScaleY(1.0f),
m_fPreviousPosX(0.0f),
m_fPreviousPosY(0.0f),
m_pkTransformationMatrix( new D3DXMATRIX() )
{
	// default transformation
	updateLocalTransformation();
}
//---------------------------------------------------------------------------
Entity2D::~Entity2D ()
{
	delete m_pkTransformationMatrix;
	m_pkTransformationMatrix = NULL;
}
//---------------------------------------------------------------------------
void Entity2D::setPos (float fPosX, float fPosY)
{
	m_fPreviousPosX = m_fPosX;
	m_fPreviousPosY = m_fPosY;

	m_fPosX = fPosX;
	m_fPosY = fPosY;

	updateLocalTransformation();
}
//---------------------------------------------------------------------------
void Entity2D::setRotation (float fRotation)
{
	m_fRotation = fRotation;

	updateLocalTransformation();
}
//---------------------------------------------------------------------------
void Entity2D::setScale (float fScaleX, float fScaleY)
{
	m_fScaleX = fScaleX;
	m_fScaleY = fScaleY;

	updateLocalTransformation();
}
//---------------------------------------------------------------------------
void Entity2D::returnToPos (float fPosX, float fPosY)
{
	m_fPosX = fPosX;
	m_fPosY = fPosY;

	updateLocalTransformation();
}
//---------------------------------------------------------------------------
void Entity2D::updateLocalTransformation ()
{
	// translation matrix
	D3DXMATRIX kTransMat;
	D3DXMatrixTranslation(&kTransMat, m_fPosX, m_fPosY, 0);

	// rotation in Z matrix
	D3DXMATRIX kRotationMat;
	D3DXMatrixRotationZ(&kRotationMat, m_fRotation);

	// scale matrix
	D3DXMATRIX kScaleMat;
	D3DXMatrixScaling(&kScaleMat, m_fScaleX, m_fScaleY, 1);

	// build matrix multiplying transformations
	D3DXMatrixIdentity(m_pkTransformationMatrix);
	D3DXMatrixMultiply(m_pkTransformationMatrix, &kTransMat, m_pkTransformationMatrix);
	D3DXMatrixMultiply(m_pkTransformationMatrix, &kRotationMat, m_pkTransformationMatrix);
	D3DXMatrixMultiply(m_pkTransformationMatrix, &kScaleMat, m_pkTransformationMatrix);
}
//---------------------------------------------------------------------------
Entity2D::CollisionResult Entity2D::checkCollision (Entity2D& rkEntity2D) const
{
	float fOverlapX = std::max( 0.0f, 
								std::min( posX() + fabs( scaleX() ) / 2.0f, 
										  rkEntity2D.posX() + fabs( rkEntity2D.scaleX() ) / 2.0f 
								) - 
								std::max( posX() - fabs( scaleX() ) / 2.0f, 
										  rkEntity2D.posX() - fabs( rkEntity2D.scaleX() ) / 2.0f 
								) 
	);

	float fOverlapY = std::max( 0.0f, 
								std::min( posY() + fabs( scaleY() ) / 2.0f, rkEntity2D.posY() + fabs( rkEntity2D.scaleY() ) / 2.0f ) - 
								std::max( posY() - fabs( scaleY() ) / 2.0f, rkEntity2D.posY() - fabs( rkEntity2D.scaleY() ) / 2.0f ) 
	);

	if(fOverlapX != 0.0f && fOverlapY != 0.0f)
	{
		if(fOverlapX > fOverlapY)
		{
			return CollisionVertical;
		}
		else
		{
			return CollisionHorizontal;
		}
	}

	return NoCollision;
}
//---------------------------------------------------------------------------
void Entity2D::drawAABB (Renderer& rkRenderer) const
{
	static ColorVertex s_akAABBVertices[5];
	static bool s_bIsInitialized = false;
	if(!s_bIsInitialized)
	{
		s_bIsInitialized = true;

		s_akAABBVertices[0].x = -0.5;	s_akAABBVertices[0].y = -0.5;	s_akAABBVertices[0].z = 0.0f;	s_akAABBVertices[0].color = PG1_COLOR_RGB(0,255,0);
		s_akAABBVertices[1].x = -0.5;	s_akAABBVertices[1].y = 0.5;	s_akAABBVertices[1].z = 0.0f;	s_akAABBVertices[1].color = PG1_COLOR_RGB(0,255,0);
		s_akAABBVertices[2].x = 0.5;	s_akAABBVertices[2].y = 0.5;	s_akAABBVertices[2].z = 0.0f;	s_akAABBVertices[2].color = PG1_COLOR_RGB(0,255,0);
		s_akAABBVertices[3].x = 0.5;	s_akAABBVertices[3].y = -0.5;	s_akAABBVertices[3].z = 0.0f;	s_akAABBVertices[3].color = PG1_COLOR_RGB(0,255,0);
		s_akAABBVertices[4].x = -0.5;	s_akAABBVertices[4].y = -0.5;	s_akAABBVertices[4].z = 0.0f;	s_akAABBVertices[4].color = PG1_COLOR_RGB(0,255,0);
	}

	// draw AABB
	rkRenderer.setCurrentTexture(NoTexture);
	rkRenderer.setMatrix( World, transformationMatrix() );
	rkRenderer.draw(s_akAABBVertices, pg1::LineStrip, 5);
}
//---------------------------------------------------------------------------
